
package com.lp.zyll.controller;



import com.lp.zyll.aop.Log;
import com.lp.zyll.domain.User;
import com.lp.zyll.domain.dto.UserDto;
import com.lp.zyll.exception.BadRequestException;
import com.lp.zyll.redis.RedisCRUD;
import com.lp.zyll.security.LoginUser;
import com.lp.zyll.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("auth")
@RequiredArgsConstructor
public class AuthorizationController {

    private final AuthenticationManagerBuilder authenticationManagerBuilder;



    @Autowired
    RedisCRUD redisCRUD;

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public ResponseEntity login(@RequestBody UserDto user){
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(user.getUsername(),user.getPassword() );
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        return ResponseEntity.ok(loginUser );

    }
    @PostMapping("/register")
    public ResponseEntity register(@RequestBody User user){

        PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        user.setPassword(passwordEncoder.encode(user.getPassword()));
//        user.setUserApi(DigestUtils.md5DigestAsHex(user.getUsername().getBytes()));
//        user.setUserPromoteCode(RandomStringUtils.randomAlphanumeric(15));

        boolean save = userService.save(user);

        return ResponseEntity.ok(save);

    }

    @Log("测试")
    @PostMapping("/test")
    public ResponseEntity test(String str){

        System.out.println();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        authentication.getPrincipal();
        return ResponseEntity.ok(authentication);
    }


    @RequestMapping("/test2")
    public ResponseEntity test2(){

        List<User> list = userService.list();

        if (1==1){
            throw  new BadRequestException("1111111111");
        }

        return ResponseEntity.ok(list);

    }

    @PostMapping("/test3")
    public ResponseEntity test3(){

        redisCRUD.set("test","14321");

        String test = redisCRUD.get("test");
        return ResponseEntity.ok(test);

    }

    @Log("单文件上传")
    @RequestMapping(value="/upload",method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public HttpEntity upload(@RequestPart("file") MultipartFile files, String title) throws IOException {

//        for (MultipartFile file:files
//             ) {

//            System.out.println(file.toString());
            System.out.println(files.toString());
//        }
        return  ResponseEntity.ok("上传成功");

    }
    @Log("多文件File上传")
    @PostMapping(value = "uploads",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity uploads(@RequestPart("files") MultipartFile[] file, String title, HttpServletRequest request) throws IOException {

//        for (MultipartFile file:files
//             ) {

//            System.out.println(file.toString());
            System.out.println(file.toString());
//        }
        return  ResponseEntity.ok("上传成功");

    }
//
//
//    @PostMapping("redis1")
//    public ResultBody redis1(String key) throws IOException {
//        HashMap map = new HashMap();
//        map.put("t1",12312);
//        map.put("t2",14535);
//        map.put("t3",5644);
//        map.put("t4",63543);
//
//        redisValUtil.set(key,map);
//
//        return ResultBody.success();
//
//    }
//
//    @Autowired
//    RedisBaseUtil redisBaseUtil;

//    @Autowired
//    RedisValUtil redisBaseUtil;
    @PostMapping("redis2")
    public ResponseEntity redis2(String key) throws IOException {
//        RedisBaseUtil redisBaseUtil = new RedisSetUtil();
//        RedisValUtil redisValUtil = new RedisValUtil();
//        redisValUtil.set(key,"测试");
//        redisValUtil.set(1,key,"测试",10);
//        redisValUtil.set(2,key,"测试",20);
//        redisValUtil.set(3,key,"测试",30);
//        redisValUtil.set(4,key,"测试",40);
//        redisValUtil.set(5,key,"测试",50);

        return ResponseEntity.ok(1);

    }


}
